
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 *
 * @author David Beaver
 */
public class Request {
    private String command;
    private Integer id;
    private HashMap<String,String> headers;
    private String xml;

    public Request(){
        this.id = new Integer(0);
        this.xml = "";
        this.command = "";
        this.headers = new HashMap<String,String>();
    }
    
    /* Returns the command given in the request */
    public String getCommand(){
        return this.command;
    }
    public String getPath(){
        if(this.headers.containsKey("Path")){
            return this.headers.get("Path");
        }
        return new String();
    }
    /* Returns the job id this request is associated with */
    public Integer getID(){
        return this.id;
    }
    
    /* Returns the xml given to describe the job */
    public String getXML(){
        return this.xml;
    }
    
    /* Parses incoming input from the socket for processing */
    public void processRequest(BufferedReader incoming){
        try{
            if(incoming.ready()){ //if the incoming request can be read
                System.out.println("EXEC: Processing request...");
                StringTokenizer parser;
                parser = new StringTokenizer(incoming.readLine()); //parse the first line
                this.command = parser.nextToken(); //save the command
                this.xml = "";
                if(this.command.equals("create-job")){ //if the command is "create-job"
                    StringTokenizer param = new StringTokenizer(parser.nextToken(),"="); //extract job id
                    param.nextToken(); //skip the variable
                    this.id = new Integer(param.nextToken()); //save the value
                    while(incoming.ready()){ //while there is more to process
                        parser = new StringTokenizer(incoming.readLine()); //parse the next line for headers
                        if(parser.hasMoreTokens()){ //if there are headers
                            String header = parser.nextToken(); //save the header
                            String tag = parser.nextToken(); //save the tagged value
                            this.headers.put(header, tag); //save the mapping
                        }else{ //if we've hit a blank line
                            //time to save save the job xml
                            while(incoming.ready()){ //while there is more to read
                                this.xml += incoming.readLine()+"\n"; //save the next line
                            }
                        }
                    }
                }else if(this.command.equals("job-status")){
                    StringTokenizer param = new StringTokenizer(parser.nextToken(),"="); //extract job id
                    param.nextToken(); //skip the variable
                    this.id = new Integer(param.nextToken()); //save the value
                    while(incoming.ready()){ //while there is more to process
                        parser = new StringTokenizer(incoming.readLine()); //parse the next line for headers
                        if(parser.hasMoreTokens()){ //if there are headers
                            String header = parser.nextToken(); //save the header
                            String tag = parser.nextToken(); //save the tagged value
                            this.headers.put(header, tag); //save the mapping
                        }
                    }
                }else{ //this is just like any other command
                    while(incoming.ready()){ //while there are headers
                        parser = new StringTokenizer(incoming.readLine()); //read the next line
                        if(parser.hasMoreTokens()){ //if there is a header
                            String header = parser.nextToken(); //save the header
                            String tag = parser.nextToken(); //save its tagged value
                            this.headers.put(header, tag); //save the mapping
                        }
                    }
                }
                /* Print the request */
                if(this.id.intValue() == 0)
                    System.out.println("EXEC:\tCommand: "+this.command);
                else
                    System.out.println("EXEC:\tCommand: "+this.command+" id="+this.id);
                for(String header : this.headers.keySet()){
                    System.out.println("EXEC:\t"+header+": "+this.headers.get(header));
                }
            }else{ //the incoming input could not be read
                
            }
        }catch(IOException ex){
            System.out.println("Could not process request");
        }
    }
}

